Imports cmplib01

Public Class frmComposeMessage
    Inherits System.Windows.Forms.Form

    Const c_Module As String = "frmComposeMessage"

    Dim ptrCurrentGame As CampaignerGameData

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Panel1 As System.Windows.Forms.Panel
    Friend WithEvents lblSelectPlayer As System.Windows.Forms.Label
    Friend WithEvents btnCancel As System.Windows.Forms.Button
    Friend WithEvents btnOK As System.Windows.Forms.Button
    Friend WithEvents pnlTopBorder As System.Windows.Forms.Panel
    Friend WithEvents picCampaignerIcon As System.Windows.Forms.PictureBox
    Friend WithEvents txtTitleBox As System.Windows.Forms.TextBox
    Friend WithEvents lstPlayers As System.Windows.Forms.ListBox
    Friend WithEvents txtTitle As System.Windows.Forms.TextBox
    Friend WithEvents txtMessageText As System.Windows.Forms.TextBox
    Friend WithEvents lblMessageTitle As System.Windows.Forms.Label
    Friend WithEvents Label1 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(frmComposeMessage))
        Me.Panel1 = New System.Windows.Forms.Panel()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.lblMessageTitle = New System.Windows.Forms.Label()
        Me.txtMessageText = New System.Windows.Forms.TextBox()
        Me.txtTitle = New System.Windows.Forms.TextBox()
        Me.lstPlayers = New System.Windows.Forms.ListBox()
        Me.lblSelectPlayer = New System.Windows.Forms.Label()
        Me.btnCancel = New System.Windows.Forms.Button()
        Me.btnOK = New System.Windows.Forms.Button()
        Me.pnlTopBorder = New System.Windows.Forms.Panel()
        Me.picCampaignerIcon = New System.Windows.Forms.PictureBox()
        Me.txtTitleBox = New System.Windows.Forms.TextBox()
        Me.Panel1.SuspendLayout()
        Me.pnlTopBorder.SuspendLayout()
        Me.SuspendLayout()
        '
        'Panel1
        '
        Me.Panel1.BackColor = System.Drawing.Color.LightSkyBlue
        Me.Panel1.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label1, Me.lblMessageTitle, Me.txtMessageText, Me.txtTitle, Me.lstPlayers, Me.lblSelectPlayer})
        Me.Panel1.Location = New System.Drawing.Point(8, 56)
        Me.Panel1.Name = "Panel1"
        Me.Panel1.Size = New System.Drawing.Size(402, 176)
        Me.Panel1.TabIndex = 20
        '
        'Label1
        '
        Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 11.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label1.ForeColor = System.Drawing.Color.Black
        Me.Label1.Location = New System.Drawing.Point(161, 56)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(110, 24)
        Me.Label1.TabIndex = 7
        Me.Label1.Text = "Message"
        '
        'lblMessageTitle
        '
        Me.lblMessageTitle.Font = New System.Drawing.Font("Microsoft Sans Serif", 11.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblMessageTitle.ForeColor = System.Drawing.Color.Black
        Me.lblMessageTitle.Location = New System.Drawing.Point(160, 8)
        Me.lblMessageTitle.Name = "lblMessageTitle"
        Me.lblMessageTitle.Size = New System.Drawing.Size(112, 24)
        Me.lblMessageTitle.TabIndex = 6
        Me.lblMessageTitle.Text = "Message Title"
        '
        'txtMessageText
        '
        Me.txtMessageText.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.txtMessageText.Enabled = False
        Me.txtMessageText.Location = New System.Drawing.Point(160, 80)
        Me.txtMessageText.Multiline = True
        Me.txtMessageText.Name = "txtMessageText"
        Me.txtMessageText.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.txtMessageText.Size = New System.Drawing.Size(232, 84)
        Me.txtMessageText.TabIndex = 5
        Me.txtMessageText.Text = "Enter Message Text"
        '
        'txtTitle
        '
        Me.txtTitle.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.txtTitle.Enabled = False
        Me.txtTitle.Location = New System.Drawing.Point(160, 32)
        Me.txtTitle.Name = "txtTitle"
        Me.txtTitle.Size = New System.Drawing.Size(232, 20)
        Me.txtTitle.TabIndex = 4
        Me.txtTitle.Text = "Enter Title"
        '
        'lstPlayers
        '
        Me.lstPlayers.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.lstPlayers.Location = New System.Drawing.Point(16, 32)
        Me.lstPlayers.Name = "lstPlayers"
        Me.lstPlayers.Size = New System.Drawing.Size(136, 132)
        Me.lstPlayers.TabIndex = 3
        '
        'lblSelectPlayer
        '
        Me.lblSelectPlayer.Font = New System.Drawing.Font("Microsoft Sans Serif", 11.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblSelectPlayer.ForeColor = System.Drawing.Color.Black
        Me.lblSelectPlayer.Location = New System.Drawing.Point(16, 8)
        Me.lblSelectPlayer.Name = "lblSelectPlayer"
        Me.lblSelectPlayer.Size = New System.Drawing.Size(112, 24)
        Me.lblSelectPlayer.TabIndex = 2
        Me.lblSelectPlayer.Text = "Select Player"
        '
        'btnCancel
        '
        Me.btnCancel.BackColor = System.Drawing.Color.LightSkyBlue
        Me.btnCancel.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnCancel.ForeColor = System.Drawing.Color.Black
        Me.btnCancel.Location = New System.Drawing.Point(328, 240)
        Me.btnCancel.Name = "btnCancel"
        Me.btnCancel.TabIndex = 19
        Me.btnCancel.Text = "Cancel"
        '
        'btnOK
        '
        Me.btnOK.BackColor = System.Drawing.Color.LightSkyBlue
        Me.btnOK.Enabled = False
        Me.btnOK.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnOK.ForeColor = System.Drawing.Color.Black
        Me.btnOK.Location = New System.Drawing.Point(248, 240)
        Me.btnOK.Name = "btnOK"
        Me.btnOK.TabIndex = 18
        Me.btnOK.Text = "OK"
        '
        'pnlTopBorder
        '
        Me.pnlTopBorder.BackColor = System.Drawing.Color.SteelBlue
        Me.pnlTopBorder.Controls.AddRange(New System.Windows.Forms.Control() {Me.picCampaignerIcon, Me.txtTitleBox})
        Me.pnlTopBorder.Location = New System.Drawing.Point(0, 8)
        Me.pnlTopBorder.Name = "pnlTopBorder"
        Me.pnlTopBorder.Size = New System.Drawing.Size(418, 40)
        Me.pnlTopBorder.TabIndex = 17
        '
        'picCampaignerIcon
        '
        Me.picCampaignerIcon.Image = CType(resources.GetObject("picCampaignerIcon.Image"), System.Drawing.Bitmap)
        Me.picCampaignerIcon.Location = New System.Drawing.Point(9, 11)
        Me.picCampaignerIcon.Name = "picCampaignerIcon"
        Me.picCampaignerIcon.Size = New System.Drawing.Size(16, 16)
        Me.picCampaignerIcon.TabIndex = 9
        Me.picCampaignerIcon.TabStop = False
        '
        'txtTitleBox
        '
        Me.txtTitleBox.BackColor = System.Drawing.Color.SteelBlue
        Me.txtTitleBox.BorderStyle = System.Windows.Forms.BorderStyle.None
        Me.txtTitleBox.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.txtTitleBox.ForeColor = System.Drawing.Color.WhiteSmoke
        Me.txtTitleBox.Location = New System.Drawing.Point(32, 8)
        Me.txtTitleBox.Multiline = True
        Me.txtTitleBox.Name = "txtTitleBox"
        Me.txtTitleBox.ReadOnly = True
        Me.txtTitleBox.Size = New System.Drawing.Size(312, 26)
        Me.txtTitleBox.TabIndex = 8
        Me.txtTitleBox.Text = "Campaigner [Compose Message]"
        '
        'frmComposeMessage
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.BackColor = System.Drawing.Color.Black
        Me.ClientSize = New System.Drawing.Size(418, 272)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Panel1, Me.btnCancel, Me.btnOK, Me.pnlTopBorder})
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow
        Me.Name = "frmComposeMessage"
        Me.ShowInTaskbar = False
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent
        Me.Panel1.ResumeLayout(False)
        Me.pnlTopBorder.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Public Sub New(ByRef gameData As CampaignerGameData)
        Me.New()

        ptrCurrentGame = gameData
        PopulatePlayers()

    End Sub

    Private Sub PopulatePlayers()
        Dim play As Player
        Dim playKey As String

        For Each playKey In ptrCurrentGame.Players.Keys
            play = ptrCurrentGame.Players.Item(playKey)
            ' get a list of the players (ignoring the current palyer)
            If Not play Is ptrCurrentGame.selectedPlayer Then
                lstPlayers.Items.Add(play)
            End If
        Next

    End Sub

    Private Sub lstPlayers_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstPlayers.SelectedIndexChanged

        If Not lstPlayers.SelectedItem Is Nothing Then
            txtTitle.Enabled = True
            txtMessageText.Enabled = True
            btnOK.Enabled = True
        End If

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Hide()

    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        ' create a new messagem and save it
        Dim msg As New Messagem()
        Dim rPlayer As Player

        rPlayer = lstPlayers.SelectedItem

        msg.Message = txtMessageText.Text
        msg.Title = txtTitle.Text
        msg.TurnActive = ptrCurrentGame.Turn + 1
        msg.Viewed = False

        msg.SendingPlayerID = ptrCurrentGame.selectedPlayer.PlayerID
        msg.ReceivingPlayerID = rPlayer.PlayerID

        AddNewMessagem(msg)

        Me.Hide()
    End Sub

    Private Sub AddNewMessagem(ByVal mess As Messagem)
        Dim done As Boolean

        Try
            Do While Not done
                Try
                    ptrCurrentGame.Messages_g.Add(mess, "MESS" & mess.MessageID)
                    done = True
                Catch argEx As ArgumentException
                    mess.BoostMCID()
                Catch ex As Exception
                    ' get us out of the death loop
                    Throw ex
                End Try
            Loop
        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try

    End Sub

End Class

